package com.jiangjiang.eshop.wms.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jiangjiang.eshop.wms.convert.ReturnGoodsInputOrderItemConvert;
import com.jiangjiang.eshop.wms.dao.ReturnGoodsInputOrderItemMapper;
import com.jiangjiang.eshop.wms.domain.ReturnGoodsInputOrderItem;
import com.jiangjiang.eshop.wms.dto.ReturnGoodsInputOrderItemDTO;
import com.jiangjiang.eshop.wms.dto.ReturnGoodsInputOrderPutOnItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author Jianghaowen
 * @Date 2021/9/28
 * @Description: TODO
 * @Version V1.0
 **/
@Service
public class ReturnGoodsInputOrderItemService extends ServiceImpl<ReturnGoodsInputOrderItemMapper, ReturnGoodsInputOrderItem> {
    @Autowired
    private ReturnGoodsInputOrderItemConvert returnGoodsInputOrderItemConvert;
    @Autowired
    private ReturnGoodsInputOrderPutOnItemService returnGoodsInputOrderPutOnItemService;

    /**
     * 退货入库明细数据保存
     *
     * @param returnGoodsInputOrderId 退货入库id
     * @param items                   退货入库明细数据
     */
    public void saveBatchReturnGoodsInputOrderItems(Long returnGoodsInputOrderId, List<ReturnGoodsInputOrderItemDTO> items) {
        items.forEach(item -> item.setReturnGoodsInputOrderId(returnGoodsInputOrderId));
        this.saveBatch(returnGoodsInputOrderItemConvert.convertDTOList2Entities(items));
    }

    /**
     * 查询退货入库单明细数据
     *
     * @param returnGoodsInputOrderId 退货入库单id
     * @return 查询结果
     */
    public List<ReturnGoodsInputOrderItemDTO> queryByReturnGoodsInputOrderId(Long returnGoodsInputOrderId) {
        LambdaQueryWrapper<ReturnGoodsInputOrderItem> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ReturnGoodsInputOrderItem::getReturnGoodsInputOrderId, returnGoodsInputOrderId);
        List<ReturnGoodsInputOrderItemDTO> dtoList = returnGoodsInputOrderItemConvert.convertEntities2DTOList(this.list(wrapper));

        Set<Long> itemIds = dtoList.stream().map(ReturnGoodsInputOrderItemDTO::getId).collect(Collectors.toSet());
        Map<Long, List<ReturnGoodsInputOrderPutOnItemDTO>> putOnItemDtoMap = returnGoodsInputOrderPutOnItemService.queryByItemIds(itemIds)
                .stream()
                .collect(Collectors.groupingBy(ReturnGoodsInputOrderPutOnItemDTO::getId));

        dtoList.forEach(itemDTO ->{
            itemDTO.setPutOnItems(putOnItemDtoMap.getOrDefault(itemDTO.getId(), Lists.newArrayList()));
        });

        return dtoList;
    }

    /**
     * 更新退货入库单明细数据
     * @param items 退货入库单明细数据
     */
    public void updateBatchReturnGoodsInputOrderItem(List<ReturnGoodsInputOrderItemDTO> items) {
        Set<ReturnGoodsInputOrderItem> itemsEntities = items.stream().map(itemDTO -> {
            ReturnGoodsInputOrderItem item = new ReturnGoodsInputOrderItem();
            item.setArrivalCount(itemDTO.getArrivalCount());
            item.setQualifiedCount(itemDTO.getQualifiedCount());
            item.setGmtModified(new Date());
            return item;
        }).collect(Collectors.toSet());
        // 批量更新
        this.updateBatchById(itemsEntities);
    }
}
